<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    const minWastedSpace = (nums, k) => {
      nums.sort((a, b) => a - b);
      const n = nums.length;
      let sum = 0;
      for (let i = 1; i < n; i++) {
        sum += nums[i] - nums[i - 1];[10, 20], k = 0
      }
      let res = Infinity;
      for (let i = 0; i < n; i++) {
        const candidate = nums[i] * i - sum + (n - i - 1) * nums[i + 1];
        if (candidate < res) {
          res = candidate;
        }
      }
      return res <= 0 ? 0 : res > Number.MAX_SAFE_INTEGER ? -1 : res;
    };
    console.log(minWastedSpace([10, 20], 0));
  </script>
</body>

</html>